package locpris.swan.ac.uk;

import java.util.ArrayList;

import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Geometry;
import com.jme.scene.Line;
import com.jme.scene.Node;
import com.jme.scene.shape.Sphere;
import com.jme.scene.shape.Sphere.TextureMode;

/*
 * A Node
 */
public class ANode {

	//the location of the node x/z/y
	private Vector3f location = new Vector3f(0.0f,0.0f,0.0f);
	//list of locations
	private ArrayList<Vector3f> locations = new ArrayList<Vector3f>();
	//a list of the node representations on plane
	//private ArrayList<Sphere> nodeLocations = new ArrayList<Sphere>();
	//a scenegraph node to link all nodes too
	public Node nodes = new Node("Nodes");
	//list of links shownig movement
	private ArrayList<Vector3f> links = new ArrayList<Vector3f>();
	//shape
	Sphere aNode = new Sphere("node");
	//number of nodes to start always = 0
	int numberNodes = 0;
	//default colour of the node
	public ColorRGBA nodeColour = ColorRGBA.blue.clone();
	public ColorRGBA linkColour = ColorRGBA.blue;
	//nodes name
	String nodeName;
	
	public ANode()
	{
		
	}
	
	public ANode(String name)
	{
		nodeName = name;
	}
	
	public ANode(Vector3f startPosition)
	{
		this.setColour("blue");
		this.setLinkColour("blue");
		aNode.setDefaultColor(nodeColour);
		this.setLocation(startPosition);
		aNode.updateGeometry(getLocation(numberNodes), 30, 30, 0.3f);
		nodes.attachChild(aNode);
	}
	
	public void addNodeLocation(Vector3f loc)
	{
		Sphere addNode = new Sphere(nodeName);
		numberNodes = getNumberOfNodes();
		//aNode.setDefaultColor(ColorRGBA.blue.clone());
		//this.setColour("blue",addNode);
		addNode.setDefaultColor(nodeColour);
		this.setLocation(loc);
		addNode.updateGeometry(getLocation(numberNodes), 30, 30, 0.3f);
		nodes.attachChild(addNode);
		//link from previous node to itself
		links.add(new Vector3f(this.getLocation(numberNodes-1)));	
		links.add(new Vector3f(loc));
		Geometry link = new Line("Link Line", links.toArray(new Vector3f[] {}), null, null, null);
		link.getDefaultColor().set(linkColour);
		nodes.attachChild(link);
	}
	
	public void setLocation(float addX, float addY, float addZ)
	{
		location.set(addX, addY, addZ);
		System.out.println("xyz="+addX+","+addY+","+addZ);
	}
	
	//returns the number of movements for this node
	public int getNumberOfNodes()
	{
		return locations.size();
	}
	
	public void setLocation(Vector3f loc)
	{
			locations.add(loc);
	}
	
	public void changeLocation(Vector3f loc)
	{
		locations.clear();
		locations.add(loc);
		
		numberNodes = getNumberOfNodes();
		Sphere addNode = new Sphere(nodeName);
		addNode.updateGeometry(getLocation(numberNodes), 30, 30, 0.3f);
		addNode.setDefaultColor(ColorRGBA.black.clone());
		nodes.attachChild(addNode);
		//link from previous node to itself
		links.add(new Vector3f(this.getLocation(numberNodes-1)));	
		links.add(new Vector3f(loc));
		Geometry link = new Line("Link Line", links.toArray(new Vector3f[] {}), null, null, null);
		link.getDefaultColor().set(linkColour);
		nodes.attachChild(link);
		addToRoot();
	}
	
	public Vector3f getLocation(int nodeNumber)
	{
		return locations.get(nodeNumber);
	}
	
	public Node getNode()
	{
		return nodes;
	}
	
	public void addToRoot()
	{
		LocPriSMain.addNode(nodes);
	}
	
	public ColorRGBA getColour()
	{
		return nodeColour;
	}
	
	public void setColour(String colour)
	{
		if (colour.compareTo("blue")==0) { nodeColour=ColorRGBA.blue.clone(); }
		if (colour.compareTo("green")==0) { nodeColour=ColorRGBA.green.clone(); }
		if (colour.compareTo("red")==0) { nodeColour=ColorRGBA.red.clone(); }
	}
	
	public void setLinkColour(String colour)
	{
		if (colour.compareTo("blue")==0) { linkColour=ColorRGBA.blue; }
		if (colour.compareTo("green")==0) { linkColour=ColorRGBA.green; }
		if (colour.compareTo("red")==0) { linkColour=ColorRGBA.red; }
	}
	
	public String getName()
	{
		return nodeName;
	}
}
